<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=11"/>
<meta name="generator" content="Doxygen 1.9.4"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>miniFOC: algorithm/foc.c Source File</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr id="projectrow">
  <td id="projectlogo"><img alt="Logo" src="Acme_LOGO.png"/></td>
  <td id="projectalign">
   <div id="projectname">miniFOC<span id="projectnumber">&#160;1.0.3</span>
   </div>
   <div id="projectbrief">This open-source project aims to accomplish a FOC(Field Oriented Control) scheme that is operatable with minimum costs in China.</div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.9.4 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
var searchBox = new SearchBox("searchBox", "search",'Search','.html');
/* @license-end */
</script>
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
$(function() {
  initMenu('',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
/* @license-end */
</script>
<div id="main-nav"></div>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div id="nav-path" class="navpath">
  <ul>
<li class="navelem"><a class="el" href="dir_99d3723365464dd656f512988bb8dbbd.html">algorithm</a></li>  </ul>
</div>
</div><!-- top -->
<div class="header">
  <div class="headertitle"><div class="title">foc.c</div></div>
</div><!--header-->
<div class="contents">
<a href="foc_8c.html">Go to the documentation of this file.</a><div class="fragment"><div class="line"><a id="l00001" name="l00001"></a><span class="lineno">    1</span><span class="comment">/**************************************************************************/</span></div>
<div class="line"><a id="l00010" name="l00010"></a><span class="lineno">   10</span><span class="preprocessor">#include &quot;<a class="code" href="foc_8h.html">foc.h</a>&quot;</span></div>
<div class="line"><a id="l00011" name="l00011"></a><span class="lineno">   11</span><span class="preprocessor">#include &quot;<a class="code" href="config_8h.html">config.h</a>&quot;</span></div>
<div class="line"><a id="l00012" name="l00012"></a><span class="lineno">   12</span><span class="preprocessor">#include &quot;<a class="code" href="fast__math_8h.html">fast_math.h</a>&quot;</span></div>
<div class="line"><a id="l00013" name="l00013"></a><span class="lineno">   13</span><span class="preprocessor">#include &quot;<a class="code" href="encoder_8h.html">encoder.h</a>&quot;</span></div>
<div class="line"><a id="l00014" name="l00014"></a><span class="lineno">   14</span><span class="preprocessor">#include &quot;<a class="code" href="system_8h.html">system.h</a>&quot;</span></div>
<div class="line"><a id="l00015" name="l00015"></a><span class="lineno">   15</span><span class="preprocessor">#include &quot;<a class="code" href="timer_8h.html">timer.h</a>&quot;</span></div>
<div class="line"><a id="l00016" name="l00016"></a><span class="lineno">   16</span> </div>
<div class="line"><a id="l00020" name="l00020"></a><span class="lineno"><a class="line" href="foc_8c.html#a6c9e1e8356380a4129a3a59d52ee1ec4">   20</a></span><span class="keyword">volatile</span> <a class="code hl_struct" href="struct_f_o_c___structure__t.html">FOC_Structure_t</a> <a class="code hl_variable" href="foc_8c.html#a6c9e1e8356380a4129a3a59d52ee1ec4">FOC_Struct</a>;</div>
<div class="line"><a id="l00024" name="l00024"></a><span class="lineno"><a class="line" href="foc_8c.html#ad335385ea3909d9485703d97735b34ca">   24</a></span><span class="keyword">volatile</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> <a class="code hl_variable" href="foc_8c.html#ad335385ea3909d9485703d97735b34ca">phase_sequence</a> = 0;</div>
<div class="line"><a id="l00028" name="l00028"></a><span class="lineno"><a class="line" href="foc_8c.html#a418bbe3f3019eaefea9f904a87cf0a05">   28</a></span><span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> <a class="code hl_variable" href="foc_8c.html#a418bbe3f3019eaefea9f904a87cf0a05">foc_parameter_available_flag</a> = 1;</div>
<div class="line"><a id="l00029" name="l00029"></a><span class="lineno">   29</span> </div>
<div class="line"><a id="l00033" name="l00033"></a><span class="lineno"><a class="line" href="foc_8c.html#a85fbd426ff2859593ed2b1ecd4a35d4b">   33</a></span><span class="keywordtype">void</span> <a class="code hl_function" href="foc_8c.html#a85fbd426ff2859593ed2b1ecd4a35d4b">foc_calibrate_phase</a>(<span class="keywordtype">void</span>) {</div>
<div class="line"><a id="l00034" name="l00034"></a><span class="lineno">   34</span>    <span class="keywordtype">unsigned</span> <span class="keywordtype">short</span> last_angle = 0, positive_counter = 0;</div>
<div class="line"><a id="l00035" name="l00035"></a><span class="lineno">   35</span>    <span class="keywordtype">float</span> u, v, w, angle = 0;</div>
<div class="line"><a id="l00036" name="l00036"></a><span class="lineno">   36</span> </div>
<div class="line"><a id="l00037" name="l00037"></a><span class="lineno">   37</span>    <span class="comment">/* set that there is only a magnetic field on the straight axis */</span></div>
<div class="line"><a id="l00038" name="l00038"></a><span class="lineno">   38</span>    <a class="code hl_function" href="foc_8c.html#a2ee2669a464e17cfaab4e084e78c4fe1">foc_calculate_dutycycle</a>(0, <a class="code hl_define" href="config_8h.html#a637f29a41ea0451a8567972018c095f0">CALI_TORQUE</a>, 0, &amp;u, &amp;v, &amp;w);</div>
<div class="line"><a id="l00039" name="l00039"></a><span class="lineno">   39</span>    <a class="code hl_function" href="timer_8c.html#ab3614c1dcfba485b2e299839d9c189e4">update_pwm_dutycycle</a>(u, v, w);</div>
<div class="line"><a id="l00040" name="l00040"></a><span class="lineno">   40</span>    <a class="code hl_function" href="system_8c.html#a837b6e9a8c812f0964f71e37ac853bff">delayms</a>(1000);</div>
<div class="line"><a id="l00041" name="l00041"></a><span class="lineno">   41</span> </div>
<div class="line"><a id="l00042" name="l00042"></a><span class="lineno">   42</span>    <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> counter = 0; counter &lt; 50; ++counter) {</div>
<div class="line"><a id="l00043" name="l00043"></a><span class="lineno">   43</span>        <span class="comment">/* measure current motor angle */</span></div>
<div class="line"><a id="l00044" name="l00044"></a><span class="lineno">   44</span>        <span class="keywordtype">unsigned</span> <span class="keywordtype">short</span> current_angle = <a class="code hl_function" href="encoder_8c.html#a28beb7b571ff9e6d39118c8d1324e154">encoder_get_mechanical_angle</a>();</div>
<div class="line"><a id="l00045" name="l00045"></a><span class="lineno">   45</span> </div>
<div class="line"><a id="l00046" name="l00046"></a><span class="lineno">   46</span>        <span class="comment">/* judge whether the current angle increases */</span></div>
<div class="line"><a id="l00047" name="l00047"></a><span class="lineno">   47</span>        <span class="keywordflow">if</span> (current_angle &gt; last_angle)</div>
<div class="line"><a id="l00048" name="l00048"></a><span class="lineno">   48</span>            positive_counter++;</div>
<div class="line"><a id="l00049" name="l00049"></a><span class="lineno">   49</span> </div>
<div class="line"><a id="l00050" name="l00050"></a><span class="lineno">   50</span>        <span class="comment">/* update last angle as current angle */</span></div>
<div class="line"><a id="l00051" name="l00051"></a><span class="lineno">   51</span>        last_angle = current_angle;</div>
<div class="line"><a id="l00052" name="l00052"></a><span class="lineno">   52</span> </div>
<div class="line"><a id="l00053" name="l00053"></a><span class="lineno">   53</span>        <span class="comment">/* set that there is only a magnetic field on the straight axis */</span></div>
<div class="line"><a id="l00054" name="l00054"></a><span class="lineno">   54</span>        <a class="code hl_function" href="foc_8c.html#a2ee2669a464e17cfaab4e084e78c4fe1">foc_calculate_dutycycle</a>(angle, <a class="code hl_define" href="config_8h.html#a637f29a41ea0451a8567972018c095f0">CALI_TORQUE</a>, 0, &amp;u, &amp;v, &amp;w);</div>
<div class="line"><a id="l00055" name="l00055"></a><span class="lineno">   55</span>        angle += 0.2f;</div>
<div class="line"><a id="l00056" name="l00056"></a><span class="lineno">   56</span>        <a class="code hl_function" href="timer_8c.html#ab3614c1dcfba485b2e299839d9c189e4">update_pwm_dutycycle</a>(u, v, w);</div>
<div class="line"><a id="l00057" name="l00057"></a><span class="lineno">   57</span>        <a class="code hl_function" href="system_8c.html#a837b6e9a8c812f0964f71e37ac853bff">delayms</a>(20);</div>
<div class="line"><a id="l00058" name="l00058"></a><span class="lineno">   58</span>    }</div>
<div class="line"><a id="l00059" name="l00059"></a><span class="lineno">   59</span>    <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> counter = 0; counter &lt; 50; ++counter) {</div>
<div class="line"><a id="l00060" name="l00060"></a><span class="lineno">   60</span>        <span class="comment">/* measure current motor angle */</span></div>
<div class="line"><a id="l00061" name="l00061"></a><span class="lineno">   61</span>        <span class="keywordtype">unsigned</span> <span class="keywordtype">short</span> current_angle = <a class="code hl_function" href="encoder_8c.html#a28beb7b571ff9e6d39118c8d1324e154">encoder_get_mechanical_angle</a>();</div>
<div class="line"><a id="l00062" name="l00062"></a><span class="lineno">   62</span> </div>
<div class="line"><a id="l00063" name="l00063"></a><span class="lineno">   63</span>        <span class="comment">/* judge whether the current angle decreases */</span></div>
<div class="line"><a id="l00064" name="l00064"></a><span class="lineno">   64</span>        <span class="keywordflow">if</span> (current_angle &lt; last_angle)</div>
<div class="line"><a id="l00065" name="l00065"></a><span class="lineno">   65</span>            positive_counter++;</div>
<div class="line"><a id="l00066" name="l00066"></a><span class="lineno">   66</span> </div>
<div class="line"><a id="l00067" name="l00067"></a><span class="lineno">   67</span>        <span class="comment">/* update last angle as current angle */</span></div>
<div class="line"><a id="l00068" name="l00068"></a><span class="lineno">   68</span>        last_angle = current_angle;</div>
<div class="line"><a id="l00069" name="l00069"></a><span class="lineno">   69</span> </div>
<div class="line"><a id="l00070" name="l00070"></a><span class="lineno">   70</span>        <span class="comment">/* set that there is only a magnetic field on the straight axis */</span></div>
<div class="line"><a id="l00071" name="l00071"></a><span class="lineno">   71</span>        <a class="code hl_function" href="foc_8c.html#a2ee2669a464e17cfaab4e084e78c4fe1">foc_calculate_dutycycle</a>(angle, <a class="code hl_define" href="config_8h.html#a637f29a41ea0451a8567972018c095f0">CALI_TORQUE</a>, 0, &amp;u, &amp;v, &amp;w);</div>
<div class="line"><a id="l00072" name="l00072"></a><span class="lineno">   72</span>        angle -= 0.2f;</div>
<div class="line"><a id="l00073" name="l00073"></a><span class="lineno">   73</span>        <a class="code hl_function" href="timer_8c.html#ab3614c1dcfba485b2e299839d9c189e4">update_pwm_dutycycle</a>(u, v, w);</div>
<div class="line"><a id="l00074" name="l00074"></a><span class="lineno">   74</span>        <a class="code hl_function" href="system_8c.html#a837b6e9a8c812f0964f71e37ac853bff">delayms</a>(20);</div>
<div class="line"><a id="l00075" name="l00075"></a><span class="lineno">   75</span>    }</div>
<div class="line"><a id="l00076" name="l00076"></a><span class="lineno">   76</span> </div>
<div class="line"><a id="l00077" name="l00077"></a><span class="lineno">   77</span>    <span class="comment">/* zero the torque in all directions to release the motor */</span></div>
<div class="line"><a id="l00078" name="l00078"></a><span class="lineno">   78</span>    <a class="code hl_function" href="foc_8c.html#a2ee2669a464e17cfaab4e084e78c4fe1">foc_calculate_dutycycle</a>(0, 0, 0, &amp;u, &amp;v, &amp;w);</div>
<div class="line"><a id="l00079" name="l00079"></a><span class="lineno">   79</span>    <a class="code hl_function" href="timer_8c.html#ab3614c1dcfba485b2e299839d9c189e4">update_pwm_dutycycle</a>(u, v, w);</div>
<div class="line"><a id="l00080" name="l00080"></a><span class="lineno">   80</span>    <a class="code hl_function" href="system_8c.html#a837b6e9a8c812f0964f71e37ac853bff">delayms</a>(300);</div>
<div class="line"><a id="l00081" name="l00081"></a><span class="lineno">   81</span> </div>
<div class="line"><a id="l00082" name="l00082"></a><span class="lineno">   82</span>    <span class="keywordflow">if</span> (positive_counter &gt;= 75)</div>
<div class="line"><a id="l00083" name="l00083"></a><span class="lineno">   83</span>        <a class="code hl_variable" href="foc_8c.html#ad335385ea3909d9485703d97735b34ca">phase_sequence</a> = 0;</div>
<div class="line"><a id="l00084" name="l00084"></a><span class="lineno">   84</span>    <span class="keywordflow">else</span> <span class="keywordflow">if</span> (positive_counter &lt; 25)</div>
<div class="line"><a id="l00085" name="l00085"></a><span class="lineno">   85</span>        <a class="code hl_variable" href="foc_8c.html#ad335385ea3909d9485703d97735b34ca">phase_sequence</a> = 1;</div>
<div class="line"><a id="l00086" name="l00086"></a><span class="lineno">   86</span>    <span class="keywordflow">else</span></div>
<div class="line"><a id="l00087" name="l00087"></a><span class="lineno">   87</span>        <span class="keywordflow">while</span> (1);</div>
<div class="line"><a id="l00088" name="l00088"></a><span class="lineno">   88</span>}</div>
<div class="line"><a id="l00089" name="l00089"></a><span class="lineno">   89</span> </div>
<div class="line"><a id="l00099" name="l00099"></a><span class="lineno"><a class="line" href="foc_8c.html#a2ee2669a464e17cfaab4e084e78c4fe1">   99</a></span><span class="keywordtype">void</span> <a class="code hl_function" href="foc_8c.html#a2ee2669a464e17cfaab4e084e78c4fe1">foc_calculate_dutycycle</a>(<span class="keywordtype">float</span> elect_angle, <span class="keywordtype">float</span> d, <span class="keywordtype">float</span> q, <span class="keywordtype">float</span> *u, <span class="keywordtype">float</span> *v, <span class="keywordtype">float</span> *w) {</div>
<div class="line"><a id="l00100" name="l00100"></a><span class="lineno">  100</span>    <span class="keywordtype">float</span> alpha, beta;</div>
<div class="line"><a id="l00101" name="l00101"></a><span class="lineno">  101</span> </div>
<div class="line"><a id="l00102" name="l00102"></a><span class="lineno">  102</span>    <span class="comment">/* fast calculation of cosine and sine value of electric angle */</span></div>
<div class="line"><a id="l00103" name="l00103"></a><span class="lineno">  103</span>    <span class="keywordtype">float</span> cf = <a class="code hl_define" href="fast__math_8h.html#a684c3efa1e89aa4ed8534c429f614aad">fast_cos</a>(elect_angle);</div>
<div class="line"><a id="l00104" name="l00104"></a><span class="lineno">  104</span>    <span class="keywordtype">float</span> sf = <a class="code hl_function" href="fast__math_8c.html#a5af7fb1bc56e2917540c1322b1ddb480">fast_sin</a>(elect_angle);</div>
<div class="line"><a id="l00105" name="l00105"></a><span class="lineno">  105</span> </div>
<div class="line"><a id="l00106" name="l00106"></a><span class="lineno">  106</span><span class="preprocessor">#if USE_SVPWM == 0</span></div>
<div class="line"><a id="l00107" name="l00107"></a><span class="lineno">  107</span>    <span class="comment">/* firstly, the inverse Park transform is calculated */</span></div>
<div class="line"><a id="l00108" name="l00108"></a><span class="lineno">  108</span>    alpha = d * cf - q * sf;</div>
<div class="line"><a id="l00109" name="l00109"></a><span class="lineno">  109</span>    beta = q * cf + d * sf;</div>
<div class="line"><a id="l00110" name="l00110"></a><span class="lineno">  110</span> </div>
<div class="line"><a id="l00111" name="l00111"></a><span class="lineno">  111</span>    <span class="comment">/* secondly, the inverse Clarke transform is calculated */</span></div>
<div class="line"><a id="l00112" name="l00112"></a><span class="lineno">  112</span>    *u = 0.5f - alpha / <a class="code hl_define" href="config_8h.html#ad3d3342321e5502fa12f14a719ce48ec">VBUS</a>;</div>
<div class="line"><a id="l00113" name="l00113"></a><span class="lineno">  113</span>    *v = 0.5f + (alpha * 0.5f - beta * 0.866025404f) / <a class="code hl_define" href="config_8h.html#ad3d3342321e5502fa12f14a719ce48ec">VBUS</a>;</div>
<div class="line"><a id="l00114" name="l00114"></a><span class="lineno">  114</span>    *w = 1.5f - *u - *v;</div>
<div class="line"><a id="l00115" name="l00115"></a><span class="lineno">  115</span><span class="preprocessor">#else</span></div>
<div class="line"><a id="l00116" name="l00116"></a><span class="lineno">  116</span>    <span class="keywordtype">float</span> tmp2, tmp3, Ta, Tb, Tc;</div>
<div class="line"><a id="l00117" name="l00117"></a><span class="lineno">  117</span>    <span class="keywordtype">char</span> vec_sector = 3;</div>
<div class="line"><a id="l00118" name="l00118"></a><span class="lineno">  118</span> </div>
<div class="line"><a id="l00119" name="l00119"></a><span class="lineno">  119</span>    <span class="comment">/* firstly, the inverse Clarke transform is calculated */</span></div>
<div class="line"><a id="l00120" name="l00120"></a><span class="lineno">  120</span>    alpha = d * cf - q * sf;</div>
<div class="line"><a id="l00121" name="l00121"></a><span class="lineno">  121</span>    beta = q * cf + d * sf;</div>
<div class="line"><a id="l00122" name="l00122"></a><span class="lineno">  122</span>    tmp2 = beta * 0.5f + alpha * 0.8660254f;</div>
<div class="line"><a id="l00123" name="l00123"></a><span class="lineno">  123</span>    tmp3 = tmp2 - beta;</div>
<div class="line"><a id="l00124" name="l00124"></a><span class="lineno">  124</span> </div>
<div class="line"><a id="l00125" name="l00125"></a><span class="lineno">  125</span>    <span class="comment">/* judge which sector the magnetic vector is in at this time */</span></div>
<div class="line"><a id="l00126" name="l00126"></a><span class="lineno">  126</span>    vec_sector = ((*(<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> *) &amp;tmp2) &gt;&gt; 31) ? vec_sector : (vec_sector - 1);</div>
<div class="line"><a id="l00127" name="l00127"></a><span class="lineno">  127</span>    vec_sector = ((*(<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> *) &amp;tmp3) &gt;&gt; 31) ? vec_sector : (vec_sector - 1);</div>
<div class="line"><a id="l00128" name="l00128"></a><span class="lineno">  128</span>    vec_sector = ((*(<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> *) &amp;beta) &gt;&gt; 31) ? (7 - vec_sector) : vec_sector;</div>
<div class="line"><a id="l00129" name="l00129"></a><span class="lineno">  129</span> </div>
<div class="line"><a id="l00130" name="l00130"></a><span class="lineno">  130</span>    <span class="comment">/* the proportion of each phase is calculated by six sector modulation method */</span></div>
<div class="line"><a id="l00131" name="l00131"></a><span class="lineno">  131</span>    <span class="keywordflow">switch</span> (vec_sector) {</div>
<div class="line"><a id="l00132" name="l00132"></a><span class="lineno">  132</span>        <span class="keywordflow">case</span> 1:</div>
<div class="line"><a id="l00133" name="l00133"></a><span class="lineno">  133</span>        <span class="keywordflow">case</span> 4: Ta = tmp2;</div>
<div class="line"><a id="l00134" name="l00134"></a><span class="lineno">  134</span>            Tb = beta - tmp3;</div>
<div class="line"><a id="l00135" name="l00135"></a><span class="lineno">  135</span>            Tc = -tmp2;</div>
<div class="line"><a id="l00136" name="l00136"></a><span class="lineno">  136</span>            <span class="keywordflow">break</span>;</div>
<div class="line"><a id="l00137" name="l00137"></a><span class="lineno">  137</span>        <span class="keywordflow">case</span> 2:</div>
<div class="line"><a id="l00138" name="l00138"></a><span class="lineno">  138</span>        <span class="keywordflow">case</span> 5:Ta = tmp3 + tmp2;</div>
<div class="line"><a id="l00139" name="l00139"></a><span class="lineno">  139</span>            Tb = beta;</div>
<div class="line"><a id="l00140" name="l00140"></a><span class="lineno">  140</span>            Tc = -beta;</div>
<div class="line"><a id="l00141" name="l00141"></a><span class="lineno">  141</span>            <span class="keywordflow">break</span>;</div>
<div class="line"><a id="l00142" name="l00142"></a><span class="lineno">  142</span>        <span class="keywordflow">default</span>:Ta = tmp3;</div>
<div class="line"><a id="l00143" name="l00143"></a><span class="lineno">  143</span>            Tb = -tmp3;</div>
<div class="line"><a id="l00144" name="l00144"></a><span class="lineno">  144</span>            Tc = -(beta + tmp2);</div>
<div class="line"><a id="l00145" name="l00145"></a><span class="lineno">  145</span>            <span class="keywordflow">break</span>;</div>
<div class="line"><a id="l00146" name="l00146"></a><span class="lineno">  146</span>    }</div>
<div class="line"><a id="l00147" name="l00147"></a><span class="lineno">  147</span> </div>
<div class="line"><a id="l00148" name="l00148"></a><span class="lineno">  148</span>    <span class="comment">/* calculate the duty cycle in center alignment mode */</span></div>
<div class="line"><a id="l00149" name="l00149"></a><span class="lineno">  149</span>    *u = (Ta / <a class="code hl_define" href="config_8h.html#ad3d3342321e5502fa12f14a719ce48ec">VBUS</a>) * 0.5f + 0.5f;</div>
<div class="line"><a id="l00150" name="l00150"></a><span class="lineno">  150</span>    *v = (Tb / <a class="code hl_define" href="config_8h.html#ad3d3342321e5502fa12f14a719ce48ec">VBUS</a>) * 0.5f + 0.5f;</div>
<div class="line"><a id="l00151" name="l00151"></a><span class="lineno">  151</span>    *w = (Tc / <a class="code hl_define" href="config_8h.html#ad3d3342321e5502fa12f14a719ce48ec">VBUS</a>) * 0.5f + 0.5f;</div>
<div class="line"><a id="l00152" name="l00152"></a><span class="lineno">  152</span><span class="preprocessor">#endif</span></div>
<div class="line"><a id="l00153" name="l00153"></a><span class="lineno">  153</span>}</div>
<div class="ttc" id="aconfig_8h_html"><div class="ttname"><a href="config_8h.html">config.h</a></div><div class="ttdoc">used to place important parameter configurations for users</div></div>
<div class="ttc" id="aconfig_8h_html_a637f29a41ea0451a8567972018c095f0"><div class="ttname"><a href="config_8h.html#a637f29a41ea0451a8567972018c095f0">CALI_TORQUE</a></div><div class="ttdeci">#define CALI_TORQUE</div><div class="ttdoc">set calibrate torque to 0.5</div><div class="ttdef"><b>Definition:</b> <a href="config_8h_source.html#l00019">config.h:19</a></div></div>
<div class="ttc" id="aconfig_8h_html_ad3d3342321e5502fa12f14a719ce48ec"><div class="ttname"><a href="config_8h.html#ad3d3342321e5502fa12f14a719ce48ec">VBUS</a></div><div class="ttdeci">#define VBUS</div><div class="ttdoc">bus voltage is 8.0V</div><div class="ttdef"><b>Definition:</b> <a href="config_8h_source.html#l00013">config.h:13</a></div></div>
<div class="ttc" id="aencoder_8c_html_a28beb7b571ff9e6d39118c8d1324e154"><div class="ttname"><a href="encoder_8c.html#a28beb7b571ff9e6d39118c8d1324e154">encoder_get_mechanical_angle</a></div><div class="ttdeci">unsigned short encoder_get_mechanical_angle(void)</div><div class="ttdoc">read mechanical angle directly from encoder</div><div class="ttdef"><b>Definition:</b> <a href="encoder_8c_source.html#l00076">encoder.c:76</a></div></div>
<div class="ttc" id="aencoder_8h_html"><div class="ttname"><a href="encoder_8h.html">encoder.h</a></div><div class="ttdoc">this is the header file of encoder.c.</div></div>
<div class="ttc" id="afast__math_8c_html_a5af7fb1bc56e2917540c1322b1ddb480"><div class="ttname"><a href="fast__math_8c.html#a5af7fb1bc56e2917540c1322b1ddb480">fast_sin</a></div><div class="ttdeci">float fast_sin(float theta)</div><div class="ttdoc">fast calculation of sine</div><div class="ttdef"><b>Definition:</b> <a href="fast__math_8c_source.html#l00072">fast_math.c:72</a></div></div>
<div class="ttc" id="afast__math_8h_html"><div class="ttname"><a href="fast__math_8h.html">fast_math.h</a></div><div class="ttdoc">this is the header file of fast_math.c, the macros of fast amplitude limiting algorithm and cosine fu...</div></div>
<div class="ttc" id="afast__math_8h_html_a684c3efa1e89aa4ed8534c429f614aad"><div class="ttname"><a href="fast__math_8h.html#a684c3efa1e89aa4ed8534c429f614aad">fast_cos</a></div><div class="ttdeci">#define fast_cos(x)</div><div class="ttdoc">fast calculation of cosine</div><div class="ttdef"><b>Definition:</b> <a href="fast__math_8h_source.html#l00027">fast_math.h:27</a></div></div>
<div class="ttc" id="afoc_8c_html_a2ee2669a464e17cfaab4e084e78c4fe1"><div class="ttname"><a href="foc_8c.html#a2ee2669a464e17cfaab4e084e78c4fe1">foc_calculate_dutycycle</a></div><div class="ttdeci">void foc_calculate_dutycycle(float elect_angle, float d, float q, float *u, float *v, float *w)</div><div class="ttdoc">calculate the corresponding three-phase PWM duty cycle under the current electrical angle</div><div class="ttdef"><b>Definition:</b> <a href="foc_8c_source.html#l00099">foc.c:99</a></div></div>
<div class="ttc" id="afoc_8c_html_a418bbe3f3019eaefea9f904a87cf0a05"><div class="ttname"><a href="foc_8c.html#a418bbe3f3019eaefea9f904a87cf0a05">foc_parameter_available_flag</a></div><div class="ttdeci">unsigned char foc_parameter_available_flag</div><div class="ttdoc">flag variable for FOC parameter availability</div><div class="ttdef"><b>Definition:</b> <a href="foc_8c_source.html#l00028">foc.c:28</a></div></div>
<div class="ttc" id="afoc_8c_html_a6c9e1e8356380a4129a3a59d52ee1ec4"><div class="ttname"><a href="foc_8c.html#a6c9e1e8356380a4129a3a59d52ee1ec4">FOC_Struct</a></div><div class="ttdeci">volatile FOC_Structure_t FOC_Struct</div><div class="ttdoc">FOC handler.</div><div class="ttdef"><b>Definition:</b> <a href="foc_8c_source.html#l00020">foc.c:20</a></div></div>
<div class="ttc" id="afoc_8c_html_a85fbd426ff2859593ed2b1ecd4a35d4b"><div class="ttname"><a href="foc_8c.html#a85fbd426ff2859593ed2b1ecd4a35d4b">foc_calibrate_phase</a></div><div class="ttdeci">void foc_calibrate_phase(void)</div><div class="ttdoc">automatic phase sequence detection and correction</div><div class="ttdef"><b>Definition:</b> <a href="foc_8c_source.html#l00033">foc.c:33</a></div></div>
<div class="ttc" id="afoc_8c_html_ad335385ea3909d9485703d97735b34ca"><div class="ttname"><a href="foc_8c.html#ad335385ea3909d9485703d97735b34ca">phase_sequence</a></div><div class="ttdeci">volatile unsigned char phase_sequence</div><div class="ttdoc">motor phase sequence flag variable</div><div class="ttdef"><b>Definition:</b> <a href="foc_8c_source.html#l00024">foc.c:24</a></div></div>
<div class="ttc" id="afoc_8h_html"><div class="ttname"><a href="foc_8h.html">foc.h</a></div><div class="ttdoc">this is the header file of foc.c, which defines the structure of FOC algorithm and angle conversion f...</div></div>
<div class="ttc" id="astruct_f_o_c___structure__t_html"><div class="ttname"><a href="struct_f_o_c___structure__t.html">FOC_Structure_t</a></div><div class="ttdoc">structure of FOC algorithm</div><div class="ttdef"><b>Definition:</b> <a href="foc_8h_source.html#l00017">foc.h:17</a></div></div>
<div class="ttc" id="asystem_8c_html_a837b6e9a8c812f0964f71e37ac853bff"><div class="ttname"><a href="system_8c.html#a837b6e9a8c812f0964f71e37ac853bff">delayms</a></div><div class="ttdeci">void delayms(unsigned long count)</div><div class="ttdoc">millisecond delay function, any time time.</div><div class="ttdef"><b>Definition:</b> <a href="system_8c_source.html#l00030">system.c:30</a></div></div>
<div class="ttc" id="asystem_8h_html"><div class="ttname"><a href="system_8h.html">system.h</a></div><div class="ttdoc">system basic function header file</div></div>
<div class="ttc" id="atimer_8c_html_ab3614c1dcfba485b2e299839d9c189e4"><div class="ttname"><a href="timer_8c.html#ab3614c1dcfba485b2e299839d9c189e4">update_pwm_dutycycle</a></div><div class="ttdeci">void update_pwm_dutycycle(float ch0, float ch1, float ch2)</div><div class="ttdoc">update timer1 ch0 1 2 duty-cycle</div><div class="ttdef"><b>Definition:</b> <a href="timer_8c_source.html#l00020">timer.c:20</a></div></div>
<div class="ttc" id="atimer_8h_html"><div class="ttname"><a href="timer_8h.html">timer.h</a></div><div class="ttdoc">this is the header file of timer.c.</div></div>
</div><!-- fragment --></div><!-- contents -->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated by&#160;<a href="https://www.doxygen.org/index.html"><img class="footer" src="doxygen.svg" width="104" height="31" alt="doxygen"/></a> 1.9.4
</small></address>
</body>
</html>
